package com.sky.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishesMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * @Author: Yosoo
 * @Date: 2025/5/17 10:12
 * @Description:
 */
@Service
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishesMapper setmealDishesMapper;

    @Override
    @Transactional
    public void save(DishDTO dishDTO) {

        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        // 新增一条菜品数据
        dishMapper.insert(dish);
        Long dishId = dish.getId();
        // 新增多条菜品口味数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (!flavors.isEmpty()) {
            flavors.forEach(flavor -> {
                flavor.setDishId(dishId);
            });
            dishFlavorMapper.batchInsert(flavors);
        }
    }

    /**
     * 菜品分页查询
     *
     * @param dishPageQueryDTO
     * @return
     * @Author: Yosoo
     * @Date: 2025/5/17 11:11
     * @Description:
     */
    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishPageQueryDTO, dish);
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<DishVO> page = dishMapper.pageQuery(dish);
        return new PageResult(page.getTotal(), page.getResult());
    }


    /**
     * 批量删除菜品
     *
     * @param ids
     * @Author: Yosoo
     * @Date: 2025/5/17 11:27
     * @Description:
     */
    @Transactional
    @Override
    public void deleteBatch(List<Long> ids) {
        // 1. 判断菜品是不是处于售卖状态，如果是则不能删除
        for (Long id : ids) {
            Dish dish = dishMapper.getById(id);
            if (Objects.equals(dish.getStatus(), StatusConstant.ENABLE)) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        // 2. 确认套餐表中有没有关联的菜品，有则不能删除
        Integer countDishes = setmealDishesMapper.countByDishIds(ids);
        if(countDishes > 0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        // 3. 删除菜品表中的数据
        dishMapper.deleteByIds(ids);
        // 4. 删除菜品口味表中的数据
        dishFlavorMapper.deleteByDishIds(ids);
    }

    /**
     * 根据id查询菜品信息和口味信息
     * @Author:     Yosoo
     * @Date:       2025/5/17 14:10
     * @Description:
     * @param id
     * @return
     */
    @Override
    public DishVO getDishWithFlavorById(Long id) {
        return dishMapper.getDishWithFlavorById(id);
    }

    /**
     * 修改菜品信息以及口味信息
     * @Author:     Yosoo
     * @Date:       2025/5/17 14:42
     * @Description:
     * @param dishDTO
     */
    @Transactional
    @Override
    public void updateDishWithFlavor(DishDTO dishDTO) {
        // 1.修改菜品信息
        Dish dish = new Dish();
        BeanUtil.copyProperties(dishDTO,dish);
        this.update(dish);
        // 2.1 删除菜品的口味表
        dishFlavorMapper.deleteByDishId(dishDTO.getId());

        // 2.2 添加新的口味表
        List<DishFlavor> flavors = dishDTO.getFlavors();
        flavors.forEach(flavor -> {
            flavor.setDishId(dishDTO.getId());
        });
        if(ObjectUtil.isNotEmpty(flavors)){
            dishFlavorMapper.batchInsert(flavors);
        }
    }

    /**
     * 根据分类查询菜品数据
     * @Author:     Yosoo
     * @Date:       2025/5/19 09:05
     * @Description:
     * @param categoryId
     * @return
     */
    @Override
    public List<DishVO> list(Long categoryId) {
        return dishMapper.list(categoryId);
    }

    @Override
    public void updateStatus(Dish dish) {
        this.update(dish);
    }

    /**
     * 修改菜品信息
     * @Author:     Yosoo
     * @Date:       2025/5/17 14:36
     * @Description:
     * @param dish
     */
    public void update(Dish dish) {
        // 1. 修改菜品表中的数据
        dishMapper.update(dish);
    }


}
